package com.parsechina.hpush.broker.parser;

import com.parsechina.hpush.broker.exception.MqttException;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;

import java.io.Serializable;

/**
 * @author linfeng
 * @since 2016/11/22
 */
public class PushMessage implements Serializable {

    private static final long serialVersionUID = -804436440637187152L;

    private MqttMessage msg;
    private MqttMessageType messageType;

    public PushMessage(MqttMessage msg) {
        this.msg = msg;
        DecoderResult decoderResult = msg.decoderResult();
        if (decoderResult.isFailure()) {
            throw new MqttException(decoderResult.cause());
        }
        this.messageType = msg.fixedHeader().messageType();
    }

    public PushMessage() {
    }

    public MqttMessage getMsg() {
        return msg;
    }

    public MqttMessageType getMessageType() {
        return messageType;
    }

    abstract static class MessageBuilder {

        abstract MqttMessage build();

    }

}
